﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;

namespace Mystery.SharePoint
{
    /// <summary>
    /// Performs the management operations on a site
    /// </summary>
    class SiteManagementOperation
    {
        SPSite _root = null;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="root">The root site of the web application</param>
        public SiteManagementOperation(SPSite root)
        {
            _root = root;
        }
        /// <summary>
        /// Performs the various management steps on a site
        /// </summary>
        /// <param name="site">The site we are maintaining</param>
        public void Execute(SPSite site)
        {
            // skip managmenet on the root site collection
            if (_root.ID == site.ID) return;

            Logger.L.AddInformation(Category.Administration, "Performing site management on {0}", site.Url);

            ISiteManagementStep[] steps = new ISiteManagementStep[]
            {
                new QuotaManagementStep(),
                new UserManagementStep()
            };
            //FUTURE: the list of steps could potentially vary depending on the type of site 

            foreach (var step in steps)
            {
                try
                {
                    if (step.Execute(_root, site) == false)
                    {
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Logger.L.AddException(Category.Administration, ex, "Error processing {0}", site.Url);
                }
            }
        }
    }
}
